//
//  WebViewController.swift
//  TalkeTv
//
//  Created by lhf on 2024/9/19.
//

import UIKit
import WebKit

class WebViewController: UIViewController,WKUIDelegate,WKNavigationDelegate {

    @IBOutlet weak var bgView: UIView!
    var titleStr = ""
    var url = ""
    
    //获取js alert事件
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {

        let alertController = UIAlertController.init(title: "提示", message: message, preferredStyle: .alert);
        alertController.addAction(UIAlertAction.init(title: "确定", style: .cancel, handler: { (action) in
            completionHandler();
        }))
        self.present(alertController, animated: true)
    }
    
    @objc func orientationDidChange( _ notification : Notification ) {
        let currentOrientation = UIDevice.current.orientation
        switch currentOrientation {
        case .portrait, .portraitUpsideDown:
            print(".portrait, .portraitUpsideDown")
            self.navigationController?.navigationBar.isHidden = false
            self.view.backgroundColor = UIColor.white
        case .landscapeLeft, .landscapeRight:
            print(".landscapeLeft, .landscapeRight")
            self.navigationController?.navigationBar.isHidden = true
            self.view.backgroundColor = UIColor.black
        default:
            break
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let webView = WKWebView()
        webView.uiDelegate = self
        webView.navigationDelegate = self
        let myRequest = URLRequest(url: URL(string: self.url)!)
        webView.load(myRequest)
        bgView.addSubview(webView)
        webView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(bgView)
        }
        
        self.title = self.titleStr
        
        print("访问的url：\(url)")
        
        NotificationCenter.default.addObserver(self,
                                      selector: #selector(orientationDidChange),
                    name: UIDevice.orientationDidChangeNotification, object: nil)
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 判断当前屏幕方向
        let isLandscape = UIApplication.shared.windows
            .first?
            .windowScene?
            .interfaceOrientation
            .isLandscape ?? false
        if isLandscape {
            self.navigationController?.navigationBar.isHidden = true
            self.view.backgroundColor = UIColor.black
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
